//
//  ZYTabBar.swift
//  ZYWeibo
//
//  Created by Mac on 15/10/26.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

class ZYTabBar: UITabBar {

    
    private let count = 5  //定义按钮的数量
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let width = bounds.width / CGFloat(count)
        let height = bounds.height
        
//        for var i = 0; i < subviews.count; i++ {
////            print(i)
//            //第0个不是UIControl
//            //设置子控件的frame
//            if(subviews[i] is UIControl){   //判断是不是uitabbarItem
//                i--
//                subviews[i].frame = CGRectMake(CGFloat(i) * width, 0, width, height)
//            }
//        }
        var index = 0
//        print(subviews.count)
        for view in subviews {
            if(view is UIControl && !(view is UIButton)){
                view.frame = CGRectMake(CGFloat(index) * width, 0, width, height)
                
                index += index == 1 ? 2 : 1
            }
        }
        
        //添加中间的按钮
        composeButton.frame = CGRectMake(2 * width, 0, width, height)
    }

    ///懒加载
    lazy var composeButton: UIButton = {
        let button = UIButton()
        // 按钮图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        // 按钮的背景
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        self.addSubview(button)
        return button
    }()
}
